package common;

import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import common.interceptor.UserInterceptor;
import common.model._MappingKit;

public class BaseConfig extends JFinalConfig {

    private static final Prop p = PropKit.use("config.properties")
            .appendIfExists("config-dev.properties");

    public static void main(String[] args) {
        UndertowServer.start(BaseConfig.class);
    }


    public void configConstant(Constants me) {
        me.setDevMode(true);
    }

    public void configRoute(Routes me) {
        me.add(new FrontRoutes());
    }

    public void configEngine(Engine me) {
        me.setDevMode(true);
        me.addSharedFunction("/common/layout.html");
    }

    public DruidPlugin getDruidPlugin() {
        return new DruidPlugin(p.get("jdbcUrl"),
                p.get("user"), p.get("password"));
    }

    public void configPlugin(Plugins me) {
        DruidPlugin dp = getDruidPlugin();
        me.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        me.add(arp);
        _MappingKit.mapping(arp);
//        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath() + "/sql");
//        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.addSqlTemplate("sql/all.sql");
        RedisPlugin redis = new RedisPlugin("user", "localhost", 6379, 900);
        me.add(redis);
    }

    public void configInterceptor(Interceptors me) {
        me.add(new UserInterceptor());
    }

    public void configHandler(Handlers me) {
    }
}
